package com.yiba.vpn.protocal;

import com.yiba.vpn.utils.CommonMethods;

import java.nio.ByteBuffer;

/**
 * Created by Administrator on 2017/11/16 0016.
 */

public class DBQHeader {
    public static byte[] mData = null;
    public static int mOffset = 0;
    static final short offset_tlen = (short) 0;
    static final short offset_type = (short) 4;
    public int tLength;
    public int type;

    public DBQHeader() {
    }

    public DBQHeader(byte[] data, int offset) {
        mData = data;
        mOffset = offset;
    }

    public static DBQHeader fromBytes(ByteBuffer buffer) {
        DBQHeader header = new DBQHeader(buffer.array(), buffer.arrayOffset() + buffer.position());
        header.tLength = CommonMethods.readReverseInt(mData, mOffset + 0);
        header.type = CommonMethods.readReverseInt(mData, mOffset + 4);
        return header;
    }

    public void toBytes(ByteBuffer buffer) {
        byte[] sLength = new byte[4];
        CommonMethods.writeReverseInt(sLength, 0, this.tLength);
        buffer.put(sLength);
        byte[] sType = new byte[4];
        CommonMethods.writeReverseInt(sType, 0, this.type);
        buffer.put(sType);
    }

}
